Imports System
Imports System.Collections.Generic
Imports System.Text
Imports Microsoft.VisualStudio.TestTools.UnitTesting
Imports System.ComponentModel

<TestClass()> _
Public Class XamlBindingFixture
	 Private Shared Function CreateInstance() As MockBase
		  Return New MockBase()
	 End Function

	 <TestMethod()> _
	 Public Sub ShouldInitializeObject()
		  Dim obj As Object = CreateInstance()
		  Dim init As ISupportInitialize = DirectCast(obj, ISupportInitialize)
		  Dim notify As ISupportInitializeNotification = DirectCast(obj, ISupportInitializeNotification)

		  Assert.IsFalse(notify.IsInitialized)

		  Dim called As New CalledHolder
		  AddHandler notify.Initialized, AddressOf called.CalledHandler

		  init.BeginInit()
		  init.EndInit()

		  Assert.IsTrue(called.Called)
		  Assert.IsTrue(notify.IsInitialized)
	 End Sub

	 <TestMethod()> _
	 Public Sub ShouldIsChangedStartFalse()
		  Dim obj As Object = CreateInstance()
		  Dim change As IChangeTracking = DirectCast(obj, IChangeTracking)

		  Assert.IsFalse(change.IsChanged)
	 End Sub

	 <TestMethod()> _
	 Public Sub ShouldIsChangedTrueOnPropertySet()
		  Dim obj As New MockBase()
		  Dim change As IChangeTracking = DirectCast(obj, IChangeTracking)

		  obj.BaseIntValue = 5

		  Assert.IsTrue(change.IsChanged)
	 End Sub

	 <TestMethod()> _
	 Public Sub ShouldIsChangedFalseAfterInitializeNoErrors()
		  Dim obj As New MockBase()
		  Dim change As IChangeTracking = DirectCast(obj, IChangeTracking)

		  obj.BaseIntValue = 5

		  obj.Initialize()

		  Assert.IsFalse(change.IsChanged)
	 End Sub

	 <TestMethod()> _
	 Public Sub ShouldIsChangedTrueIfNestedChanged()
		  Dim obj As New MockBase()
		  Dim change As IChangeTracking = DirectCast(obj, IChangeTracking)

		  Assert.IsFalse(change.IsChanged)

		  obj.BaseObject = New MockBase()

		  Assert.IsTrue(change.IsChanged)

		  obj.Initialize()

		  Assert.IsFalse(change.IsChanged)

		  DirectCast(obj.BaseObject, MockBase).BaseIntValue = 5

		  Assert.IsTrue(change.IsChanged)
	 End Sub

	 <TestMethod()> _
	 Public Sub ShouldIsInitializedFalseIfChildNotInitialized()
		  Dim obj As New MockBase()
		  Dim init As ISupportInitialize = DirectCast(obj, ISupportInitialize)
		  Dim notify As ISupportInitializeNotification = DirectCast(obj, ISupportInitializeNotification)

		  init.BeginInit()
		  init.EndInit()

		  Assert.IsTrue(notify.IsInitialized)

		  obj.BaseObject = New MockBase()

		  Assert.IsFalse(notify.IsInitialized)

		  Dim childInit As ISupportInitialize = DirectCast(obj.BaseObject, ISupportInitialize)
		  childInit.BeginInit()
		  childInit.EndInit()

		  Assert.IsTrue(notify.IsInitialized)
	 End Sub

	 <ExpectedException(GetType(InvalidOperationException))> _
	 <TestMethod()> _
	 Public Sub ShouldThrowIfEndInitCalledBeforeBeginInit()
		  Dim init As ISupportInitialize = DirectCast(CreateInstance(), ISupportInitialize)

		  init.EndInit()
	 End Sub

	 <TestMethod()> _
	 Public Sub ShouldRaiseTypedPropertyChanged()
		  Dim obj As New MockBase()

		  Dim called As New CalledHolder
		  AddHandler obj.BaseIntValueChanged, AddressOf called.CalledHandler

		  obj.BaseIntValue = 5

		  Assert.IsTrue(called.Called)
	 End Sub

	 <TestMethod()> _
	 Public Sub ShouldRaiseGenericPropertyChanged()
		  Dim obj As New MockBase()
		  Dim called As New CalledHolder
		  AddHandler obj.PropertyChanged, AddressOf called.CalledHandler

		  obj.BaseIntValue = 5

		  Assert.IsTrue(called.Called)
	 End Sub


	<TestMethod()> _
	 Public Sub ShouldReportNestedChangedProperties()
		Dim derived As New MockDerived()
		derived.DerivedIntValue = 5

		Assert.IsTrue(derived.IsChanged)

		derived.Initialize()
		Assert.IsFalse(derived.IsChanged)

		derived.DerivedObject = New MockBase()
		Assert.IsTrue(derived.IsChanged)

		derived.Initialize()
		Assert.IsFalse(derived.IsChanged)

		DirectCast((derived.DerivedObject), MockBase).BaseIntValue = 10
		Assert.IsTrue(derived.IsChanged)
	End Sub

	 <TestMethod()> _
	 Public Sub ShouldReportNestedIsInitialized()
		  Dim derived As New MockDerived()

		  Assert.IsFalse(DirectCast(derived, ISupportInitializeNotification).IsInitialized)

		  derived.Initialize()
		  Assert.IsTrue(DirectCast(derived, ISupportInitializeNotification).IsInitialized)

		  derived.DerivedObject = New MockBase()
		  Assert.IsFalse(DirectCast(derived, ISupportInitializeNotification).IsInitialized)

		  derived.Initialize()
		  Assert.IsTrue(DirectCast(derived, ISupportInitializeNotification).IsInitialized)
	End Sub



	Private Class CalledHolder

		Private _called As Boolean
		Public Property Called() As Boolean
			Get
				Return _called
			End Get
			Set(ByVal value As Boolean)
				_called = value
			End Set
		End Property

		Public Sub CalledHandler(ByVal sender As Object, ByVal args As EventArgs)
			_called = True
		End Sub

		Public Sub CalledHandler(ByVal sender As Object, ByVal args As PropertyChangedEventArgs)
			_called = True
		End Sub
	End Class
End Class
